package systems.jMenuBuilder.elements.items;

import java.awt.event.ActionEvent;
import java.util.HashMap;

import javax.swing.ButtonGroup;
import javax.swing.ImageIcon;
import javax.swing.JRadioButtonMenuItem;
import javax.swing.KeyStroke;

import org.jdom.Element;

import systems.languages.MultiLanguages;

public class RadioButtonMenuItem
    extends JRadioButtonMenuItem
    implements Items {

  private static final long serialVersionUID = 1L;
  
  private static HashMap<String, ButtonGroup> buttonGroupMap = new HashMap<String, ButtonGroup>();
  private String nameEntryKey;
  private String icoPath;
  private String tooltipEntryKey;
  private char acceleratorKey;
  private String acceleratorMask;
  private char mnemonicKey;
  private boolean selected;
  private String groupName;
  
  public RadioButtonMenuItem()
  {
    super();
  }
  
  public RadioButtonMenuItem(String nameEntryKey, String icoPath,
                          String tooltipEntryKey, char acceleratorKey,
                          String acceleratorMask, char mnemonicKey,
                          boolean selected )
  {
    super();
    //Save the generals data relative to the menu.
    this.nameEntryKey = nameEntryKey;
    this.icoPath = icoPath;
    this.tooltipEntryKey = tooltipEntryKey;
    this.acceleratorKey = Character.toUpperCase(acceleratorKey);
    this.acceleratorMask = acceleratorMask;
    this.mnemonicKey = mnemonicKey;
    this.selected = selected;
    //DEBUG TRACE
    //END OF TRACE
    super.setText(MultiLanguages.getProperties(this.nameEntryKey));
    
    if(this.icoPath != null)
      super.setIcon(new ImageIcon(this.icoPath));
    
    if(this.tooltipEntryKey != null)
      super.setToolTipText(MultiLanguages.getProperties(this.nameEntryKey));
    
    if(this.acceleratorKey != 0)
    { 
      if(this.acceleratorMask.equals("ctrl"))
        super.setAccelerator(
            KeyStroke.getKeyStroke(
                this.acceleratorKey, ActionEvent.CTRL_MASK));
      else if(this.acceleratorMask.equals("alt"))
        super.setAccelerator(
            KeyStroke.getKeyStroke(
                this.acceleratorKey, ActionEvent.ALT_MASK));
      else if(this.acceleratorMask.equals("shift"))
        super.setAccelerator(
            KeyStroke.getKeyStroke(
                this.acceleratorKey, ActionEvent.SHIFT_MASK));
    }
    super.setMnemonic(this.mnemonicKey);
    super.setSelected(this.selected);
  }

  public RadioButtonMenuItem(Element item)
  {
    super();
    //Save the generals data relative to the menu.
    this.nameEntryKey =     item.getChildText("name");
    this.icoPath =          item.getAttributeValue("ico");
    this.tooltipEntryKey =  item.getAttributeValue("tooltip");
    String tmpAcceleratorKey = item.getAttributeValue("accelerator_key");
    if(tmpAcceleratorKey != null)
      this.acceleratorKey = tmpAcceleratorKey.toUpperCase().charAt(0);
    this.acceleratorMask =  item.getAttributeValue("accelerator_mask");
    String tmpMnemonicKey = item.getAttributeValue("mnemonic_key");
    if(tmpMnemonicKey != null)
      this.mnemonicKey =    tmpMnemonicKey.toUpperCase().charAt(0);
    this.selected =         new Boolean(item.getAttributeValue("selected"));
    this.groupName =        item.getAttributeValue("group");
    //DEBUG TRACE
    //END OF TRACE
    
    this.update();
    
    if(this.icoPath != null)
      super.setIcon(new ImageIcon(this.icoPath));
    
    if(this.acceleratorKey != 0)
    { 
      if(this.acceleratorMask.equals("ctrl"))
        super.setAccelerator(
            KeyStroke.getKeyStroke(
                this.acceleratorKey, ActionEvent.CTRL_MASK));
      else if(this.acceleratorMask.equals("alt"))
        super.setAccelerator(
            KeyStroke.getKeyStroke(
                this.acceleratorKey, ActionEvent.ALT_MASK));
      else if(this.acceleratorMask.equals("shift"))
        super.setAccelerator(
            KeyStroke.getKeyStroke(
                this.acceleratorKey, ActionEvent.SHIFT_MASK));
    }
    super.setMnemonic(this.mnemonicKey);
    if(!buttonGroupMap.containsKey(this.groupName))
      buttonGroupMap.put(this.groupName, new ButtonGroup());
    setGroup(this.groupName);
  }
  
  public void setGroup(String name)
  {
     ButtonGroup group = buttonGroupMap.get(name);
     group.add(this);
  }
  
  public void update()
  {
    setText(MultiLanguages.getProperties(this.nameEntryKey));
    if(this.tooltipEntryKey != null)
      super.setToolTipText(MultiLanguages.getProperties(this.tooltipEntryKey));
  }
}
